package code.service;

import code.entity.User;
import code.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserService {

    public static void saveUser(User user) throws SQLException {

        String sql = "insert into user (id,username,password,createTime) values(?,?,?,?)";

        Connection con = DBUtil.getConn(true);
        PreparedStatement psmt = con.prepareStatement(sql);
        psmt.setInt(1, 0);
        psmt.setString(2, user.getUsername());
        psmt.setString(3, user.getPassword());
        psmt.setTimestamp(4, new Timestamp(new Date().getTime()));

        psmt.executeUpdate();
    }

    public static boolean hasUser(String username,String password) throws SQLException {
        String sql="select * from user where username=? and password=?";
        Connection con = DBUtil.getConn(true);
        PreparedStatement psmt = con.prepareStatement(sql);
        psmt.setString(1, username);
        psmt.setString(2, password);

        ResultSet rs=psmt.executeQuery();

        return rs.next();
    }

}
